package com.woniu.service.impl;

import com.woniu.dao.EmpDao;
import com.woniu.entity.*;
import com.woniu.service.EmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpDao empDao;

    @Override
    public void add(Emp emp) {
        empDao.insert(emp);
    }

    @Override
    public List<Emp> listByPage(PageInfo pageInfo) {
        Integer countRow = Integer.parseInt(empDao.countByExample(null) +"");
        pageInfo.setCountRow(countRow);
        pageInfo.setCountPage(pageInfo.getCountRow()%pageInfo.getPageSize()==0?pageInfo.getCountRow()/pageInfo.getPageSize(): pageInfo.getCountRow()/ pageInfo.getPageSize()+1);
        EmpExample example = new EmpExample();
        example.setLimit(pageInfo.getPageSize());
        example.setOffset((long) ((pageInfo.getPageNum()-1)* pageInfo.getPageSize()));
        List<Emp> empList = empDao.selectByExample(example);
        return empList;
    }

    @Override
    public int  deleteSelect(List<Integer> empnoes) {
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria =  empExample.createCriteria();
        criteria.andEmpnoIn(empnoes);
       int i = empDao.deleteByExample(empExample);
        System.out.println(i);
       return i;

    }

    @Override
    public Emp findById(int empno) {
        Emp emp = empDao.selectByPrimaryKey(empno);
        return emp;
    }

    @Override
    public Emp findByName(Emp emp) {
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria  = empExample.createCriteria();
        criteria.andEnameLike(emp.getEname());
        List<Emp> emp1 =  empDao.selectByExample(empExample);
        Emp emp2=emp1.get(0);
        return emp2;
    }

    @Override
    public void updateMoney(Integer id,Integer money) {
      Emp emp=  findById(id);
      emp.setMgr(money);
      empDao.updateByPrimaryKey(emp);
    }

    @Override
    public void updateDept(Integer id,Integer deptno) {
        Emp emp=  findById(id);
        emp.setDeptno(deptno);
        empDao.updateByPrimaryKey(emp);
    }
}
